import httpRequest from '@/utils/httpRequest'
import constant from '../constant'
import axios from 'axios'
import Vue from 'vue'


export function saveDecisions (data) {
  return httpRequest({
    url: constant.DR + '/rule/ruleDecisionHeaderInfo/save',
    method: 'post',
    data: data
  })
}
export function getDecisionsByGroup (data) {
  return httpRequest({
    url: constant.DR + '/rule/ruleDecisionHeaderInfo/getByGroup',
    method: 'post',
    data: data
  })
}
export function downTemplate ( options = {}) {
  // return httpRequest({
  //   url: constant.DR + '/rule/ruleDecisionHeaderInfo/downTemplate',
  //   method: 'post',
  //   data: data
  // })
  let url = constant.DR + '/rule/ruleDecisionHeaderInfo/downTemplate'

  return new Promise((resolve, reject) => {
    // console.log(`${url} 请求数据，参数=>`, JSON.stringify(options))
    axios.defaults.headers['content-type'] = 'application/json;charset=UTF-8'
    axios.defaults.headers['token']= Vue.cookie.get('token')
    axios({
      method: 'post',
      url: url, // 请求地址
      data: options, // 参数
      responseType: 'blob' // 表明返回服务器返回的数据类型
    }).then(
      response => {
        resolve(response.data)
        let blob = new Blob([response.data], {
          type: 'application/vnd.ms-excel'
        })
        let fileName = Date.parse(new Date()) + '.xlsx'
        if (window.navigator.msSaveOrOpenBlob) {
          // console.log(2)
          navigator.msSaveBlob(blob, fileName)
        } else {
          // console.log(3)
          var link = document.createElement('a')
          link.href = window.URL.createObjectURL(blob)
          link.download = fileName
          document.body.appendChild(link)
          link.click()
          document.body.removeChild(link)
          //释放内存
          window.URL.revokeObjectURL(link.href)
        }
      },
      err => {
        reject(err)
      }
    )
  })
}
export function doImport (data) {
  return httpRequest({
    url: constant.DR + '/rule/ruleDecisionHeaderInfo/doImport',
    method: 'post',
    data: data
  })
}
export function downAllTemplate ( options = {}) {
  // return httpRequest({
  //   url: constant.DR + '/rule/ruleDecisionHeaderInfo/downTemplate',
  //   method: 'post',
  //   data: data
  // })
  let url = constant.DR + '/rule/ruleDecisionHeaderInfo/downAllTemplate'

  return new Promise((resolve, reject) => {
    // console.log(`${url} 请求数据，参数=>`, JSON.stringify(options))
    axios.defaults.headers['content-type'] = 'application/json;charset=UTF-8'
    axios.defaults.headers['token']= Vue.cookie.get('token')
    axios({
      method: 'post',
      url: url, // 请求地址
      data: options, // 参数
      responseType: 'blob' // 表明返回服务器返回的数据类型
    }).then(
      response => {
        // debugger
        resolve(response.data)
        let blob = new Blob([response.data], {
          type: 'application/vnd.ms-excel'
        })
        let fileName = Date.parse(new Date()) + '.xls'
        if (window.navigator.msSaveOrOpenBlob) {
          // console.log(2)
          navigator.msSaveBlob(blob, fileName)
        } else {
          // console.log(3)
          var link = document.createElement('a')
          link.href = window.URL.createObjectURL(blob)
          link.download = fileName
          document.body.appendChild(link)
          link.click()
          document.body.removeChild(link)
          //释放内存
          window.URL.revokeObjectURL(link.href)
        }
      },
      err => {
        reject(err)
      }
    )
  })
}
